/**
 * @details: 
 * @brief: 
 * @version: 1.0
 * @author: liuri
 * @Date: 2021-10-05 03:53:56
 * @LastEditors: liuri
 * @LastEditTime: 2021-10-05 05:19:27
 * @FilePath: /TestProject/cPath/vtysh/cmd_readline.c
 * @Copyright (C) 2021 liuri. All rights reserved.
 */


#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/un.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <pwd.h>
#include <stdlib.h>

#define MAXPATHLEN          4096
static char *line_read;
char history_file[MAXPATHLEN];

char *vtysh_readline_gets (char *prompt)
{
    HIST_ENTRY *last;
    /* If the buffer has already been allocated, return the memory
    * to the free pool. */
    if (line_read)
    {
        free (line_read);
        line_read = NULL;
    }
     
    /* Get a line from the user.  Change prompt according to node.  XXX. */
    line_read = readline (prompt);
     
    /* If the line has any text in it, save it on the history. But only if
    * last command in history isn't the same one. */
    if (line_read && *line_read)
    {
        // 使用 readline的历史记录
        using_history();

        // 获取上一次的历史纪律
        last = previous_history();
        if (!last || strcmp (last->line, line_read) != 0) {
            add_history (line_read);
            append_history(1,history_file);
        }
    }

    return (line_read);
}
